Function Basics

  • Function

    1. What is a function

    A function is a block of statements that can be used repeatedly in a program.

    A function will not execute automatically when a page loads.

    A function will be executed by a call to the function.

    A function should be defined before the function call.

    A function take informations as parameter, executes a block of statements or perform operations on this parameters and returns the result.


    2. Why should we use functions?

    Reusability: If we have a common code that we would like to use at various parts of a program, we can simply contain it within a function and call it whenever required. This reduces the time and effort of repetition of a single code. This can be done both within a program and also by importing the PHP file, containing the function, in some other program

    Easier error detection: Since, our code is divided into functions, we can easily detect in which function, the error could lie and fix them fast and easily.

    Easily maintained: As we have used functions in our program, so if anything or any line of code needs to be changed, we can easily change it inside the function and the change will be reflected everywhere, where the function is called. Hence, easy to maintain.


    3. Creating a Function

    Syntax:
    
                      function function_name(){
                          executable code;
                      }
    
                      
    Example
    
                        <?php 
    
                        //function definition
                        function showMessage()
                        {
                            echo "Hi Guys";
                        }
                        ?>
    
                      

    4. Call a Function

    A function will not execute automatically. So we need to call the function using function_name()

    Syntax:
    
                      function_name();
    
                      
    Example
    
                        <?php 
                          // function call
                          showMessage();;
                        ?>
    
                      
    Complete code is given
    
                        <?php 
                        // function definition
                        function showMessage()
                        {
                            echo "Hi Guys";
                        }
    
    
                        //function call
                        showMessage();
    
                        ?>
    
                      

    5. Call function between html tags

    
                        <?php 
                        function showMessage()
                        {
                            echo "Hi Guys";
                        }
    
    
    
                        ?>
    
                        <table>
                            <tr>
                                <td>show output of function here</td>
                                <td>
                                <?php 
                                      showMessage();
                                  ?>
                                  </td>
                           </tr>
                        </table>